﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dev.Control;
using SfcorpDLL.biz;
using SfcorpDLL.ety;
using Billing.ety;
using Billing;
using GT.ORM;

namespace Sfcorp.form
{
    public partial class frmBillingMonth : BaseFormDev
    {
        bizBillingMonth bzBillingMonth;
        FactoryBill bzBill;
        etyBillParams eyBillRq = new etyBillParams();

        public frmBillingMonth()
        {
            InitializeComponent();
        }

        private void barRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshDate();
        }

        //刷新
        public override etyQuickKey RefreshDate()
        {
            if (tvBillComp.FocusedNode == null)
                return base.RefreshDate();
            etyFrtAgyBns ety = tvBillComp.FocusedNode.Tag as etyFrtAgyBns;
            if (ety == null)
                return base.RefreshDate();
            Refresh(ety);
            return base.RefreshDate();
        }

        public void Refresh(etyFrtAgyBns ety)
        {
            try { splashScreenManager1.ShowWaitForm(); }
            catch { }

            IList<etyFrtAgyBns> ilAngBns = bzBillingMonth.GetBillBnsList(ety);

            srcBinding.DataSource = ilAngBns;
            ggcBilling.DataSource = srcBinding;
            ggcBilling.AutoColumnWidth();
            try { splashScreenManager1.CloseWaitForm(); }
            catch { }
        }

        private void frmAccountBilling_Load(object sender, EventArgs e)
        {
            bzBillingMonth = new bizBillingMonth(global.basSysPara);

            string sGridHead = "[CONTRACT_DATE;接单日期],[CONTRACT_NO;业务单号],[PORT_NAM;码头名称],[CUSTOMER_NAM;客户名称],[I_E_NAM;出货形式],[FOREIGN_VVD_DESC;内外贸],[WORK_WAY_NAM;作业类型],[SEA_BILL_NO;提单号],[BOX20;20尺],[BOX40;40尺],[BOX45;45尺],[BOXS;柜数],[TEU;TEU],[REC_NAM;创建人],[CNTR;柜号],[I_AMOUNT;应收],[O_AMOUNT;应付],[REMARK;备注]";
            ggcBilling.ColumnsMappingCreate(sGridHead);
            this.winUtil.LoadGridLayout(ggcBilling);
            ggcBilling.GridView.OptionsView.ShowGroupPanel = true;

            //初始化计费
            eyBillRq.sqlHelp = global.basSysPara.SqlHelp;
            eyBillRq.sqliteHelp = global.basSysPara.SqliBaseHelp;
            eyBillRq.PORT_CODE = global.basSysPara.PORT_CODE;
            eyBillRq.LST_USER = global.basSysPara.UserID;
            eyBillRq.BILL_NAME = "SfcorpBill";
            eyBillRq.CHARGE_MOD = "MONTH";
            bzBill = new FactoryBill(eyBillRq);

            IList<etyFrtAgyBns> ilComp = bzBillingMonth.GetBillComp(new etyFrtAgyBns());
            tvBillComp.DataSource = ilComp;
        }

        private void barBilling_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            IList<etyFrtAgyBns> ilAngBns = ggcBilling.SelectRows<etyFrtAgyBns>();
            if (ilAngBns.Count <= 0) return;

            try{  splashScreenManager1.ShowWaitForm();   }
            catch { }
            //刷新界面所需要的数据
            TT.Module.RefreshTable.BaseTableRefreshByTables("BILL_CODE,CODE_REF,VW_BILL_PRICE,VW_BILL_PROJECT", false);
            //初始化计费
            IList<etyBillParams> lstBill = new List<etyBillParams>();
            etyBillParams eyBillRq = new etyBillParams();

            foreach (etyFrtAgyBns ey in ilAngBns)
            {
                eyBillRq = new etyBillParams();
                eyBillRq.BILL_KEY1 = ey.CONTRACT_NO;
                eyBillRq.PORT_CODE = global.basSysPara.PORT_CODE;
                lstBill.Add(eyBillRq);
            }

            etyBillParams eyBillPara = new etyBillParams();
            eyBillPara.BillSourceList = lstBill;

            eyBillPara = bzBill.Billing(eyBillPara);

            IList<etyChargeDtl> ilChrg = bzBill.GetBilledDtl(eyBillRq);
            DataTable dtChrg = bzBill.GetBilledDtl_DataTable(eyBillRq);
            dtChrg = TT.Common.dataProcessor.ColumnMoves(dtChrg, "ITEM_DESC,CHRG_DESC1 PORT_NAM,CHRG_FLT20 CONTRACT_NO,CHRG_DESC22 CLIENT_NAM,CHRG_DESC5 WORK_WAY_NAM,CHRG_FLT25 CNTR_TYPE,IO_FLAG_DESC,AMOUNT");
            dtChrg = TT.Common.dataProcessor.DataTableCross(dtChrg, "Cross", "ITEM_DESC", "AMOUNT", 0,"","");

            //eyBillRq.BILL_KEY1 = eyBnsHdr.FABC_GKEY;
            //eyBillRq.BILL_KEY2 = eyBnsHdr.CONTRACT_NO;
            //eyBillRq.CUSTOMER = eyBnsHdr.CUSTOMER_ID;

            frmAgyBnsBillMain fmBill = new frmAgyBnsBillMain(ilChrg, eyBillRq, type_view_bill.accBill, dtChrg, bzBill);
            fmBill.ShowDialog();

            try { splashScreenManager1.CloseWaitForm(); }
            catch { }
        }

        private void ggcBilling_DoubleClick(object sender, EventArgs e)
        {
            etyFrtAgyBns ey = srcBinding.Current as etyFrtAgyBns;
            if (ey == null)
                return;
            try { splashScreenManager1.ShowWaitForm(); }
            catch { }
            MainForm mainForm = this.MdiParent as MainForm;
            DevExpress.XtraBars.Docking2010.Views.BaseDocument document = null;
            Sfcorp.form.frmAgyBns frmBns;
            if (mainForm.Documents.TryGetValue("Bns", out document))
            {
                frmBns = document.Form as Sfcorp.form.frmAgyBns;
                frmBns.Close();
            }
            frmBns = new Sfcorp.form.frmAgyBns();
            frmBns.ContractNo = ey.CONTRACT_NO;
            frmBns.MOD_ID = "barBns";
            frmBns.MdiParent = this.MdiParent;
            frmBns.Show();
            try { splashScreenManager1.CloseWaitForm(); }
            catch { }
        }

        private void barExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            F11();
        }

        public override etyQuickKey F11()
        {
            this.Close();
            return base.F11();
        }

        private void tvBillComp_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (e.Node == null)
                return;
            etyFrtAgyBns ety = e.Node.Tag as etyFrtAgyBns;
            if (ety == null)
                return;
            Refresh(ety);
        }
    }
}
